//箭头飞行的演示程序。箭头旋转着飞舞的动态效果，让新手引导变得灵动。
Class FlyArrow extends Box
var arrow;
var hit;
var toDir=2;
//构造函数。生成箭头和目标圆盘。
function FlyArrow(){
	G.main.addChild(this);
	this.setSize("f","f");
	this.addChild([
		[{dim:"hit",pos:["c","c",80,80],pivot:[40,40],bg:[,"#666",40],shadow:[0,0,20,"#666",true]},
			[new Label("Hit","#666"),{pos:["c","c"]}],
		],
		["example.fly-arrow",{dim:"arrow",pos:["f/2",100],rotation:-90,pivot:[0,38]}]
	]);
	this.hit.transit({alpha:[0,1],scale:[0,1],ease:1,delay:300,time:500})
}
//让箭头飞行。反复自调用，传递临时构建的动画数据，把动画分割成几步，逐步实现飞行全过程。
function fly(m){
	if(!m){
		m={step:1};
		this.setEnabled(false);
		this.toDir++;
	}
	else if(m.step==1){
		var x=this.hit.x;
		var y=this.hit.y;
		this.arrow.set({
			x:this.arrow.x+(x-this.arrow.x)/15,
			y:this.arrow.y+(y-this.arrow.y)/15,
			rotation:this.arrow.rotation+((this.toDir-3)*90-this.arrow.rotation)/15,
		});
		if(Math.distance(this.arrow.x,this.arrow.y,x,y)<1){
			m.step++;
			m.time=G.now;
			this.hit.transit({alpha:0,scale:0,ease:2});
		}
	}
	else if(m.step==2){
		if(G.now-m.time>=600){
			m.step++;
			m.time=G.now;
			this.setRandomPos();
			this.hit.transit({alpha:1,scale:1,ease:1});
		}
	}
	else if(m.step==3){
		if(G.now-m.time>=600){
			m.step=0;
			this.setEnabled(true);
		}
	}
	if(m.step>0) callLater([this,this.fly,m],1);
}
//设置随机位置。如果与点击区距离过近，则重新设置位置
function setRandomPos(){
	var x=int(G.w*(0.8*Math.random()+0.1));
	var y=int(G.h*(0.8*Math.random()+0.1));
	if(Math.distance(this.hit.x,this.hit.y,x,y)<300){
		this.setRandomPos();
		return;
	}
	this.hit.setPos(x,y);
}
//点击事件。点击目标时让箭头飞行。
function onClick(){
	if(G.target==this.hit) this.fly();
}